//
//  HiScore.mm
//  crazy_checkers
//
//  Created by Dmitriy Tatarinov on 11/4/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "HiScore.h"
#import "game.h"
#import "game2p.h"
#import "vars.h"
#import "menu.h"
#import "gameover.h"

void ScoreStage::Load()
{
	
	xFlushTouches();
	// load fonts
	_mainFont             = xLoadFont("mainfont");
	
	img_score=xLoadImage("score.png");
//	img_dig=xLoadAnimImage("dig.png",25, 36, 0, 10);	
	img_dig2=xLoadAnimImage("dig2.png",13, 24, 0, 10);
	
	switch(g_color){
		case 1:
			img_color=xLoadAnimImage("b_w_3.png",64,32,0,2);			
			break;
		case 2:
			img_color=xLoadAnimImage("g_v_3.png",64,32,0,2);
			break;
		case 3:
			img_color=xLoadAnimImage("g_p_3.png",64,32,0,2);			
			break;
		case 4:
			img_color=xLoadAnimImage("r_b_3.png",64,32,0,2);			
			break;
	}
	
	//score1=800-lastGameBlackCount*100;
	//score2=800-lastGameWhiteCount*100;
	
	score1=g_score1;
	score2=g_score2;
	
	total_score1=total_score1+score1+lastGameWhiteCount*100;
	total_score2=total_score2+score2+lastGameBlackCount*100;	
	
	snd_pick=xLoadSound("pick.m4a");
	
	if (soundPresent==true){
		xSoundVolume(snd_pick, 1);
	}else{
		xSoundVolume(snd_pick, 0);
	}
	
	// set stage as active
	MakeActive();
}

void ScoreStage::Update()
{
	//check_Touch();
	xUpdateWorld(1.0);
	if (xCountTouches() > 0)
	{
		xPlaySound(snd_pick);
		
		Stage* next;
		
		if (quick_game==1){
			total_score1=0;
			total_score2=0;
			mode=4;
			next = new MenuStage;//GameoverStage();
		} else {
		
		if (whiteStartLine < 0 | blackStartLine < 0)
		{
			if (gameProhod < PROHOD_COUNT){
				gameProhod++;
				blackStartLine = 0;
				whiteStartLine = 0;
				if (numPlayers == 1) next = new GameStage;
				else next = new GameStage2p;
				level_cnt = 1;
			}else{
				if(numPlayers==1 & (total_score1<total_score2)) {
					next = new MenuStage();
				}else{
					next = new GameoverStage;
				}
			}
		}
		else{
			if (numPlayers == 1) next = new GameStage;
			else next = new GameStage2p;
			level_cnt++;
		}
		}
		next->Load();
	}
}

void ScoreStage::Render()
{
	
	//xRenderWorld();
	
	xDrawImage(img_score, 0, 0, 0);
	
	xDrawImage(img_color, 30, 93, 1);
	xDrawImage(img_color, 230, 93, 0);
	
	int frame1l,frame1m,frame1r,frame2l,frame2m,frame2r;	
	
	frame1l=level_cnt/10;
	frame1r=level_cnt%10;
	
	xDrawImage(img_dig2, 147, 97, frame1l);
	xDrawImage(img_dig2, 162, 97, frame1r);
	
	
	//wins
	
	frame1l=whiteVictoryCount/10;
	frame1r=whiteVictoryCount%10;
	
	xDrawImage(img_dig2, 14, 160, frame1l);	
	xDrawImage(img_dig2, 29, 160, frame1r);
	
	frame2l=blackVictoryCount/10;
	frame2r=blackVictoryCount%10;
	
	xDrawImage(img_dig2, 277, 160, frame2l);	
	xDrawImage(img_dig2, 291, 160, frame2r);	
	
	//score
	frame1r=(score1/100)%10;
	frame1l=score1/1000;
	
	frame2r=(score2/100)%10;
	frame2l=score2/1000;	
	
	xDrawImage(img_dig2, 15, 236, frame1l);	
	xDrawImage(img_dig2, 30, 236, frame1r);	
	xDrawImage(img_dig2, 45, 236, 0);	
	xDrawImage(img_dig2, 60, 236, 0);
	
	xDrawImage(img_dig2, 250, 236, frame2l);	
	xDrawImage(img_dig2, 265, 236, frame2r);	
	xDrawImage(img_dig2, 280, 236, 0);	
	xDrawImage(img_dig2, 295, 236, 0);	
	
	//bonus
	xDrawImage(img_dig2, 79, 308, lastGameWhiteCount);	
	xDrawImage(img_dig2, 13, 308, 1);	
	xDrawImage(img_dig2, 28, 308, 0);	
	xDrawImage(img_dig2, 43, 308, 0);
	
	xDrawImage(img_dig2, 225, 308, lastGameBlackCount);	
	xDrawImage(img_dig2, 264, 308, 1);	
	xDrawImage(img_dig2, 279, 308, 0);	
	xDrawImage(img_dig2, 294, 308, 0);	
	
	//total
	
	
	frame1l=total_score1/10000;
	frame1m=(total_score1/1000)%10;
	frame1r=(total_score1/100)%10;
	
	frame2l=total_score2/10000;
	frame2m=(total_score2/1000)%10;
	frame2r=(total_score2/100)%10;	
	
	xDrawImage(img_dig2, 13, 380, frame1l);	
	xDrawImage(img_dig2, 28, 380, frame1m);
	xDrawImage(img_dig2, 43, 380, frame1r);
	xDrawImage(img_dig2, 58, 380, 0);
	xDrawImage(img_dig2, 73, 380, 0);	
	
	xDrawImage(img_dig2, 232, 380, frame2l);	
	xDrawImage(img_dig2, 247, 380, frame2m);	
	xDrawImage(img_dig2, 262, 380, frame2r);
	xDrawImage(img_dig2, 277, 380, 0);
	xDrawImage(img_dig2, 292, 380, 0);
	
	// draw text information
/*	xSetFont(_mainFont);
	char buff[256];
	sprintf(buff, "X: %i \n Y: %i", dxx,dyy);
	xText(5, 5, buff, false, false);
 */
    xFlip();	
}

void ScoreStage::Unload()
{
	xFreeFont(_mainFont);
	xFreeImage(img_score);
//	xFreeImage(img_dig);
	xFreeImage(img_dig2);
	xFreeImage(img_color);
	xFreeSound(snd_pick);
	_mainFont = img_score = img_dig2 = img_color = snd_pick = NULL;
	xFlushTouches();	
}
